package com.ruoyi.education.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.students.domain.Awards;
import com.ruoyi.students.domain.Students;
import com.ruoyi.students.service.IStudentsService;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.education.domain.Score;
import com.ruoyi.education.service.IScoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生成绩录入Controller
 *
 * @author yxy
 * @date 2024-10-19
 */
@RestController
@RequestMapping("/score/score")
public class ScoreController extends BaseController {
    @Autowired
    private IScoreService scoreService;
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private IStudentsService studentsService;

    /**
     * 查询学生成绩录入列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Score score) {
        startPage();
        List<Score> list = scoreService.selectScoreList(score);
        return getDataTable(list);
    }

    /**
     * 导出学生成绩录入列表
     */
    @PreAuthorize("@ss.hasPermi('score:score:export')")
    @Log(title = "学生成绩录入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Score score) {
        List<Score> list = scoreService.selectScoreList(score);
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        util.exportExcel(response, list, "学生成绩录入数据");
    }

    /**
     * 获取学生成绩录入详细信息
     */
    @PreAuthorize("@ss.hasPermi('score:score:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(scoreService.selectScoreByID(ID));
    }

    /**
     * 新增学生成绩录入
     */
    @PreAuthorize("@ss.hasPermi('score:score:add')")
    @Log(title = "学生成绩录入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Score score) {
        System.out.println(score);
        return toAjax(scoreService.insertScore(score));
    }

    /**
     * 修改学生成绩录入
     */
    @PreAuthorize("@ss.hasPermi('score:score:edit')")
    @Log(title = "学生成绩录入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Score score) {
        return toAjax(scoreService.updateScore(score));
    }

    /**
     * 删除学生成绩录入
     */
    @PreAuthorize("@ss.hasPermi('score:score:remove')")
    @Log(title = "学生成绩录入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(scoreService.deleteScoreByIDs(IDs));
    }

    /**
     * 导入学生成绩信息 ,批量导入
     */

    @PreAuthorize("@ss.hasPermi('score:score:add')")
    @Log(title = "成绩信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Score> util = new ExcelUtil<Score>(Score.class);
        List<Score> userList = util.importExcel(file.getInputStream());
        return toAjax(scoreService.batchInsertscoreRequests(userList));
    }

    /**
     * 查询老师下面的学生的成绩
     */
    @Anonymous
    @GetMapping(value = "/listByTeacher/{teacherId}")
    public TableDataInfo listByTeacher(@PathVariable("teacherId") String teacherId,
                                    @ModelAttribute Score score) {
        startPage();
        // 根据教师id查询老师信息
        Teacher teacher = new Teacher();
        teacher.setTeacherid(teacherId);
        List<Teacher> teacherlist = teacherService.selectTeacherList(teacher);
        Teacher Thisteacher = teacherlist.get(0);

        // 查找学生
        Students students = new Students();
        students.setStudentclass(Thisteacher.getClassss());
        students.setYearAdmission(Thisteacher.getYearAdmission());
        students.setScchoolMeaasge(Thisteacher.getSchool());
        List<Students> studentlist = studentsService.selectStudentsList(students);

        List<Score> scoreList = new ArrayList<>();
        for (Students student : studentlist) {
            String studentId = student.getStudentId();
            score.setStudentID(studentId);
            List<Score> scores = scoreService.selectScoreList(score);
            scoreList.addAll(scores);
        }
        return getDataTable(scoreList);
    }

}
